# ============================================================
# Appendix A — Tables (A1–A6)
# Using Replication folders
# ============================================================

# ---- Paths ----
base_dir <- "~/Dropbox/Issue voting Chile"
data_dir <- file.path(base_dir, "09_replication/data")
outdir   <- file.path(base_dir, "09_replication/output")
dir.create(outdir, showWarnings = FALSE, recursive = TRUE)

# ---- Libraries ----
library(tidyverse)
library(haven)
library(janitor)

# ---- Helper ----
as_char <- function(x) as.character(as_factor(x))

# ============================================================
# A1. Ideology transitions between waves 2 and 3 (panel_long.dta)
# ============================================================
d <- read_dta(file.path(data_dir, "panel_long.dta"))

d2 <- d %>%
  filter(panel_all == 1, wave %in% c(2, 3)) %>%
  transmute(
    ideology     = as_char(ideology),
    CodPanelista = CodPanelista,
    wave         = wave
  )

wide <- d2 %>%
  distinct(CodPanelista, wave, .keep_all = TRUE) %>%
  pivot_wider(
    id_cols = CodPanelista,
    names_from = wave,
    values_from = ideology,
    names_glue = "ideology{wave}"
  )

if (!"ideology2" %in% names(wide)) wide$ideology2 <- NA_character_
if (!"ideology3" %in% names(wide)) wide$ideology3 <- NA_character_

wide <- wide %>%
  mutate(change = NA_integer_) %>%
  mutate(
    change = ifelse(ideology2 == "Left"  & ideology3 == "Right", 1L, change),
    change = ifelse(ideology2 == "Right" & ideology3 == "Left",  1L, change),
    change = ifelse(ideology2 == "Center"      & ideology3 == "Independent", 2L, change),
    change = ifelse(ideology2 == "Independent" & ideology3 == "Center",      2L, change),
    change = ifelse(ideology2 == "Center" & ideology3 == "Left",  3L, change),
    change = ifelse(ideology2 == "Left"   & ideology3 == "Center",3L, change),
    change = ifelse(ideology2 == "Center" & ideology3 == "Right", 4L, change),
    change = ifelse(ideology2 == "Right"  & ideology3 == "Center",4L, change),
    change = ifelse(ideology2 == "Independent" & ideology3 == "Left", 5L, change),
    change = ifelse(ideology2 == "Left"        & ideology3 == "Independent", 5L, change),
    change = ifelse(ideology2 == "Independent" & ideology3 == "Right", 6L, change),
    change = ifelse(ideology2 == "Right"       & ideology3 == "Independent", 6L, change),
    change = ifelse(ideology2 == "Left"        & ideology3 == "Left",        7L, change),
    change = ifelse(ideology2 == "Right"       & ideology3 == "Right",       8L, change),
    change = ifelse(ideology2 == "Center"      & ideology3 == "Center",      9L, change),
    change = ifelse(ideology2 == "Independent" & ideology3 == "Independent",10L, change)
  )

change_labs <- c(
  "Left <-> Right","Center  <->  Ind","Center  <-> Left","Center <-> Right",
  "Ind <-> Left","Ind <-> Right","Left","Right","Center","Ind"
)

tab_A1_change <- wide %>%
  mutate(change = factor(change, levels = 1:10, labels = change_labs)) %>%
  tabyl(change) %>%
  adorn_pct_formatting(digits = 1)

tab_A1_change


# ============================================================
# A2. A20_wave × ideology (wave 1; exclude Independent/Center)
# ============================================================
data_a2 <- read_dta(file.path(data_dir, "panel_long.dta"))

filtered_data <- data_a2 %>%
  filter(
    wave == 1,
    A20_wave != "No sé",
    !A20_wave %in% c("Independent", "Center")
  ) %>%
  mutate(
    ideology = as_char(ideology),
    A20_wave = as_char(A20_wave)
  )

ct_counts <- with(filtered_data, table(A20_wave, ideology))
ct_colpct <- sweep(ct_counts, 2, colSums(ct_counts), FUN = "/") * 100

total_row <- matrix(rep(100, ncol(ct_colpct)), nrow = 1,
                    dimnames = list("Total", colnames(ct_colpct)))
ct_with_total_row <- rbind(ct_colpct, total_row)

row_overall <- rowSums(ct_counts) / sum(ct_counts) * 100
total_col   <- c(row_overall, 100)

crosstab_A2 <- as.data.frame.matrix(round(ct_with_total_row, 1)) %>%
  rownames_to_column("A20_wave") %>%
  mutate(Total = round(total_col, 1))

crosstab_A2


# ============================================================
# A4. Simple frequencies (wave2_conjoint_merged.dta)
# ============================================================
wave2_conjoint <- read_dta(file.path(data_dir, "wave2_conjoint_merged.dta"))

table(wave2_conjoint$left_pro_mig_wave2)
table(wave2_conjoint$left_alternatve_mig)
table(wave2_conjoint$right_anti_mig_wave2)
table(wave2_conjoint$right_alternatve_mig)


# ============================================================
# A5–A6. Column-% crosstabs of A63_wave and A59_wave by ideology (wave 3)
# Collapse "No recuerdo" into missing
# ============================================================
d_a5 <- read_dta(file.path(data_dir, "panel_long.dta")) %>%
  filter(wave == 3, DEVICE_wave3 != "")

a12_chr <- as_char(d_a5$A12_wave)
a12_num <- suppressWarnings(as.numeric(a12_chr))
a12_num[which(a12_chr == "Left")]  <- 1
a12_num[which(a12_chr == "Right")] <- 10

d_a5 <- d_a5 %>%
  mutate(
    ideology_num = a12_num,
    ideology_cat = cut(
      ideology_num,
      breaks = c(0, 4, 6, 10),
      labels = c("Left", "Center", "Right"),
      include.lowest = TRUE, right = TRUE
    ),
    A63_wave = na_if(as_char(A63_wave), "No recuerdo"),
    A59_wave = na_if(as_char(A59_wave), "No recuerdo")
  )

ct_colpct_tbl <- function(data, row_var, col_var) {
  data %>%
    transmute(
      row = as_factor({{ row_var }}),
      col = factor({{ col_var }}, levels = c("Left","Center","Right"))
    ) %>%
    filter(!is.na(row), !is.na(col)) %>%
    tabyl(row, col) %>%
    adorn_totals(c("row","col")) %>%
    adorn_percentages("col") %>%
    adorn_pct_formatting(digits = 1)
}

tab_A63 <- ct_colpct_tbl(d_a5, A63_wave, ideology_cat)  # A5
tab_A59 <- ct_colpct_tbl(d_a5, A59_wave, ideology_cat)  # A6

tab_A63
tab_A59


